package com.mark.mall.web.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mark.mall.common.myException.MyException;
import com.mark.mall.common.resultBean.R;
import com.mark.mall.web.platform.dto.param.MemberLoginParam;
import com.mark.mall.web.platform.dto.param.MemberRegisterParam;
import com.mark.mall.web.platform.dto.param.MemberUpdateParam;
import com.mark.mall.web.platform.entity.Member;
import com.mark.mall.web.platform.enums.SexEnum;
import com.mark.mall.web.platform.mapper.MemberMapper;
import com.mark.mall.web.platform.service.MemberService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-03-03
 */
@Service
@DS("master")
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Override
    public R<Boolean> login(MemberLoginParam param) {
        // 根据会员名称获取会员对象
        Member member = baseMapper.selectOne(Wrappers.<Member>lambdaQuery().eq(Member::getName, param.getName()));
        if (member == null) {
            throw new MyException("会员名不存在请核实！");
        }
        // 判断密码是否一致
        if (!StrUtil.equals(SecureUtil.md5(param.getPassword() + member.getSalt()), member.getPassword())) {
            throw new MyException("密码错误，请重试！");
        }
        // 将会员id返回给前端，让其调用其它接口时传入会员id
        return new R<>(true, member.getId());
    }

    @DS("slave1")
    @Override
    public R<Boolean> register(MemberRegisterParam param) {
        // 将前端传入的参数进行赋值
        Member member = BeanUtil.copyProperties(param, Member.class);
        // 生成盐值，保证密码安全
        String salt = IdWorker.getIdStr();
        // 使用md5加密
        member.setPassword(SecureUtil.md5(param.getPassword() + salt));
        member.setSalt(salt);
        // 利用枚举进行性别插入
        member.setSex("1".equals(param.getSex()) ? SexEnum.MAN : SexEnum.WOMAN);
        // 插入数据库，并提交事务
        baseMapper.insert(member);
        return new R<>(true, "注册成功！");
    }

    @Override
    public R<Boolean> updatePassword(MemberUpdateParam param) {
        // 根据会员id获取会员对象
        Member member = baseMapper.selectById(param.getId());
        if (member == null) {
            throw new MyException("id为{" + param.getId() + "}的会员不存在！");
        }
        // 修改密码要判断旧密码是否与库中一致
        if (!StrUtil.equals(member.getPassword(), SecureUtil.md5(param.getOldPassword() + member.getSalt()))) {
            throw new MyException("密码错误，请重新输入！");
        }
        // 使用mybatis-plus修改方法修改对象
        update(Wrappers.<Member>lambdaUpdate()
                .set(Member::getPassword, SecureUtil.md5(param.getNewPassword() + member.getSalt()))
                .eq(Member::getId, param.getId()));
        return new R<>(true, "修改成功！");
    }

    @Override
    public R<Member> memberDetail(String id) {
        if (StrUtil.isBlank(id)) {
            throw new MyException("参数确实，请核实！");
        }
        return new R<>(baseMapper.selectOne(new QueryWrapper<Member>().lambda().eq(Member::getId, id)));
    }

    @Override
    public R<Boolean> doSomethingByModelPost(String type, String memberId) {
        if (StrUtil.isBlank(type) || StrUtil.isBlank(memberId)) {
            throw new MyException("参数确实，请核实！");
        }
        Member member = new Member();
        switch (type) {
            case "1":
                member.setName("李晨楠");
                member.setMail("928523947@qq.com");
                member.setMobile("15639720785");
                member.insert();
                break;
            case "2":
                member.setId(memberId);
                member.deleteById();
                break;
            case "3":
                member.update(Wrappers.<Member>lambdaUpdate().set(Member::getName, "测试").eq(Member::getId, memberId));
                break;
            case "4":
                member.setId(memberId);
                System.out.println(member.selectById());
                break;
            default:
                break;
        }
        return new R<>(true, "操作成功");
    }
}
